# Copyright (c) 2015 The Johns Hopkins University/Applied Physics Laboratory
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import logging
import sys

from kmip.core import enums
from kmip.demos import utils
from kmip.pie import client


if __name__ == '__main__':
    # 创建一个控制台日志记录器，日志级别为INFO
    logger = utils.build_console_logger(logging.INFO)

    # 创建一个命令行解析器，用于解析获取属性列表操作的参数
    parser = utils.build_cli_parser(enums.Operation.GET_ATTRIBUTE_LIST)
    # 解析命令行参数
    opts, args = parser.parse_args(sys.argv[1:])

    # 获取配置信息
    config = opts.config
    # 获取UUID
    uid = opts.uuid

    # 如果没有提供UUID，则记录错误日志并提前退出
    if uid is None:
        logger.error('No ID provided, exiting early from demo')
        sys.exit()

    # 创建一个Kmip客户端并连接到服务器
    with client.ProxyKmipClient(
            config=config,
            config_file=opts.config_file
    ) as client:
        try:
            # 获取指定对象的属性列表
            attribute_names = client.get_attribute_list(uid)
            # 记录成功获取的日志
            logger.info("Successfully retrieved {0} attribute names:".format(
                len(attribute_names)))
            # 遍历属性列表，打印每个属性名
            for attribute_name in attribute_names:
                logger.info("Attribute name: {0}".format(attribute_name))
        except Exception as e:
            # 如果出现异常，记录错误日志
            logger.error(e)
